import pymongo
import bson
import json
from bson import Binary, Code,json_util
import re


myclient = pymongo.MongoClient("mongodb://localhost:27017/")
mydb = myclient["scrapy"]
myc1 = mydb["collections1"]
#myc1.insert_one({"name":"教程"})
dblist = mydb.list_collection_names()
print(dblist)
L=''
datalists = myc1.find()
word_dict = {}
stop_words={'the',"The","","What","Who"}

dataaggr = myc1.aggregate([
    {
        '$group': {
            '_id': '$name', 
            'fcount': {
                '$count': {}
            }
        }
    }, {
        '$sort': {
            'fcount': -1
        }
    }
])
# 用list 作为获取数据，这个速度快些
#print(list(dataaggr))
# for x in datalists:
#     tmp = json_util.dumps(x)
#     data = json.loads(tmp)
    #print(data)
    # str_words= re.split(" |-|,",data['name'])
    # for str_word in str_words:
    #     if str_word in word_dict.keys():#如果key存在则value加1
    #         word_dict[str_word] = word_dict[str_word] + 1
    #     else:
    #         word_dict[str_word] = 1
    # if data["name"] in word_dict.keys():
    #     word_dict[data['name']] = word_dict[data['name']] + 1 
    # else:
    #     word_dict[data['name']] = 1
    #print(word_dict)
#word_list = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)[0:30]
## mongodb 多个值直接插入value里面可能引起速度变慢，考虑还是用collection做
### 这里定义id的作为顺序其实没有用，
# word_list = sorted(word_dict.items(), key=lambda x: x[1], reverse=True)
# w_dict = {}
# w_list = []
# for i , x in enumerate(word_list):
#     y={}
#     y.update({"_id":i})
#     y.update({"name":x[0]})
#     y.update({"count":x[1]})
#     w_list.append(y)
# c2 = mydb["collections2"]
# try:
#     c2.insert_many(w_list)
# except:
#     c2.update_many(w_list)
# print(word_list)

